package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.bo.admin.goods.GoodsCreateBO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.goods.GoodsCatAndBrandVO;
import com.cskaoyan.bean.vo.admin.goods.GoodsDetailVO;
import com.cskaoyan.service.admin.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/goods")  //  商品管理模块
public class GoodController {

    @Autowired
    GoodsService goodsService;


    @GetMapping("list")  //list 接口 列表
    public BaseRespVo<CommonData> list(String goodsSn, Integer goodsId, String name, BasePageInfo basePageInfo) {
        if (null == basePageInfo.getPage()) {
            List<MarketGoods> marketGoods = null;
            return BaseRespVo.ok(marketGoods);
        }
        CommonData<MarketGoods> query = goodsService.query(goodsSn, goodsId, name, basePageInfo);

        return BaseRespVo.ok(query);
    }

    @GetMapping("catAndBrand")  // 商品上架接口
    public BaseRespVo catAndBrand() {
        GoodsCatAndBrandVO catAndBrandVO = goodsService.getCatAndBrand();
        return BaseRespVo.ok(catAndBrandVO);
    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody GoodsCreateBO goodsCreateBO) {
        String unit = goodsCreateBO.getGoods().getUnit();
        if(!"件".equals(unit) && !"个".equals(unit) && !"盒".equals(unit)){
            return BaseRespVo.error(612, "商品单位不合法!");
        }

        int res = goodsService.createGoods(goodsCreateBO);

        if (res == 611) {
            return BaseRespVo.error(611, "商品名或商品编号已存在");
        }

        if (res == 401) {
            return BaseRespVo.error(401, "参数不能为空");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody GoodsCreateBO goodsCreateBO) {
        String unit = goodsCreateBO.getGoods().getUnit();
        if(!"件".equals(unit) || !"个".equals(unit) || !"盒".equals(unit)){
            return BaseRespVo.error(612, "商品单位不合法!");
        }

        int res = goodsService.updateGoods(goodsCreateBO);

        if (res == 611) {
            return BaseRespVo.error(611, "商品名或商品编号已存在");
        }

        if (res == 401) {
            return BaseRespVo.error(401, "参数不能为空");
        }
        return BaseRespVo.ok();
    }


    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketGoods marketGoods) {
        goodsService.delete(marketGoods);

        return BaseRespVo.ok();
    }

    @GetMapping("detail")
    public BaseRespVo detail(Integer id) {
        GoodsDetailVO goodsDetailVO = goodsService.selectGoodDetailVOById(id);
        return BaseRespVo.ok(goodsDetailVO);
    }
}
